package com.example.permission.myjbm.model;

import android.graphics.Bitmap;
import android.widget.TextView;

import com.example.permission.myjbm.dao.BaseObj;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.util.Hashtable;

/**
 * Created by JBMhaier on 2017/3/15.
 */

public class IntegralDetailModel extends BaseObj {
    /**
     * 默认初始化header
     *
     * @param headerRight
     * @param headerTitle
     * @param title
     * @param commit
     */
    public IntegralDetailModel(TextView headerRight, TextView headerTitle, String title, String commit) {
        super(headerRight, headerTitle, title, commit);
    }

    public Bitmap generateBitmap(String content, int width, int height) {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        Hashtable<EncodeHintType, String> hints = new Hashtable<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        try {
            BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            int[] pixels = new int[width * height];
            for (int i = 0; i < height; i++) {
                for (int j = 0; j < width; j++) {
                    if (encode.get(j, i)) {
                        pixels[i * width + j] = 0x00000000;
                    } else {
                        pixels[i * width + j] = 0xffffffff;
                    }
                }
            }
            return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return null;
    }
}
